/**
 * Created by leo on 15/6/24.
 */
(function (window, document, requestContent, responsePattern) {
    var parent = window.parent,
        head = document.getElementsByTagName('head')[0],
        attachEvent = window.attachEvent;
    if (window !== parent && parent === window.top && !window[requestContent]) {
        window[requestContent] = true;
        var onResponse = function (event) {
            if (event.source === parent && typeof event.data === 'string') {
                var match = event.data.match(responsePattern);
                if (match != null) {
                    var element = document.createElement('script');
                    element.setAttribute('src', match[1]);
                    head.appendChild(element);
                }
            }
        };

        if (attachEvent) {// 低版本IE
            attachEvent('onmessage', onResponse);
        } else {
            window.addEventListener('message', onResponse);
        }
        parent.postMessage(requestContent, '*');
    }
}(window, document, 'REQUEST:PORTAL_API', /^RESPONSE:PORTAL_API=(https?:\/\/.*)$/));